package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.*;
import com.itheima.health.dao.RoleMenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.RoleMenu;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
@Slf4j
public class MenuServiceImpl implements MenuService {



    @Autowired
    RoleMenuDao roleMenuDao;

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    UserDao userDao;

    @Autowired
    private UserRoleDao userRoleDao;
    /**
     * @Author: 涵哥
     * @Date: 19:33 2021/1/19
     * @Parms [queryPageBean]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 菜单-分页展示
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[菜单-分页查询]data:{}",queryPageBean);
        //调用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //分页查询
        Example example = new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();
        //如果输入查询条件不为空
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())){

            criteria.orLike("id","%"+queryPageBean.getQueryString()+"%");
            criteria.orLike("name","%"+queryPageBean.getQueryString()+"%");
        }

        Page<Menu> page = (Page<Menu>) menuDao.selectByExample(example);
        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * @Author: 涵哥
     * @Date: 20:22 2021/1/19
     * @Parms [menu]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 菜单-新增
     */
    @Override
    public void add(Menu menu) {
        log.info("[菜单-新增]data: {}",menu);
        menuDao.insertSelective(menu);
    }


    /**
     * @Author: 涵哥
     * @Date: 20:25 2021/1/19
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 菜单-删除
     */
    @Override
    public void delete(Integer id) {
        log.info("[菜单-删除]data: {}",id);
        //在删除前查询菜单是否被role表使用
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setMenuId(id);
        int i = roleMenuDao.selectCount(roleMenu);
        if (i>0){
            throw new BusinessRuntimeException("该数据无法删除,请重新操作");
        }
        menuDao.deleteByPrimaryKey(id);
    }


    /**
     * @Author: 涵哥
     * @Date: 20:37 2021/1/19
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 菜单-编辑
     */
    @Override
    public void edit(Menu menu) {
        log.info("[菜单-编辑]data: {}",menu);
        menuDao.updateByPrimaryKeySelective(menu);
    }


    /**
     * @Author: 涵哥
     * @Date: 21:09 2021/1/19
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 据id查询-编辑回显数据
     */
    @Override
    public Menu findById(Integer id) {
        Menu menu = menuDao.selectByPrimaryKey(id);
        return menu;
    }



    @Override
    public List<Map<String,Object>> showMenu() {
        log.info("查询-菜单");
        List<Map<String, Object>> mapArrayList = new ArrayList<Map<String, Object>>();
        List<Menu> menuList = menuDao.findAll(1);
        List<Map<String, Object>> mapArrayList2 = null;
        for (Menu menu1 : menuList) {
            Map<String, Object> map = new HashMap<>();
            mapArrayList2 = new ArrayList<>();
            Integer menu1Id = menu1.getId();
            List<Menu> menuList2 = menuDao.findAll(2);
            for (Menu menu2 : menuList2) {
                Map<String, Object> map2 = new HashMap<>();
                Integer parentMenuId = menu2.getParentMenuId();
                if (parentMenuId==menu1Id){
                    map2.put("path",menu2.getPath());
                    map2.put("title",menu2.getName());
                    map2.put("linkUrl",menu2.getLinkUrl());
                    map2.put("children",null);
                    mapArrayList2.add(map2);
                }
            }
            map.put("path",menu1.getPath());
            map.put("title",menu1.getName());
            map.put("icon",menu1.getIcon());
            map.put("children",mapArrayList2);
            mapArrayList.add(map);

        }
        return mapArrayList;
    }

    @Override
    public List<Menu> showMenuTwo(String username) {
        User user = userDao.selectByUsername(username);

        List<Role> roles = roleDao.selectByUserId(user.getId());//获取角色集合

        List<Menu> menus = new ArrayList<>();//父菜单

        for (Role role : roles) {
            Integer roleId = role.getId();//获取角色id

            //查询出父菜单
            List<Menu> fuMenus= menuDao.selectByLevelAndRoleId(roleId,1);
            for (Menu fuMenu : fuMenus) {
                fuMenu.getId();
                //根据父查询子
                List<Menu> ziMenus = menuDao.selectByLevelAndFuMenuId(fuMenu.getId());
                //将子赋值给父
                fuMenu.setChildren(ziMenus);
                menus.add(fuMenu);
            }
        }
        //将子菜单赋值给父菜单
        return menus;
    }
}
